﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace TowerDefence.Entities
{
    public interface ISpace
    {
        void AddEntity(Entity _entity);
        void RemoveEntity(Entity _entity);
        Entity GetEntity(Vector2 _position);
    }

    /// <summary>
    /// A space is a set of entities.
    /// The basic implementation simply adds and removes entities from a list.    
    /// </summary>
    public class Space : ISpace
    {
        private List<Entity> entities;

        public Space()
        {
            this.entities = new List<Entity>();
        }

        public void AddEntity(Entity _entity)
        {
            this.entities.Add(_entity);
        }

        public void RemoveEntity(Entity _entity)
        {
            if (this.entities.Contains(_entity))
            {
                this.entities.Remove(_entity);
            }            
        }

        public Entity GetEntity(Vector2 _position)
        {            
            var entitiesAtPosition = 
                from entity in this.entities
                where entity.Form.Position == _position
                select entity;
            if (entitiesAtPosition.Count() == 0)
            {
                return null;
            }
            else
            {
                return entitiesAtPosition.First();
            }
        }
    }
}
